package com.hackerrank.contests.oct13.challenges.angrychildren;

import java.util.Arrays;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt(), K = in.nextInt(), c[] = new int[N], m = Integer.MAX_VALUE;
		for(int n = 0; n < N; n++)
			c[n] = in.nextInt();
		Arrays.sort(c);
		for(int i = 0; i <= N - K; i++) {
			int d = c[i + K - 1] - c[i];
			if(d < m)
				m = d;
		}
		System.out.println(m);
	}
}
